package com.hxy.example.layoutmanager;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;
import com.hxy.example.utils.DensityUtils;
import com.hxy.example.utils.DisplayUtils;


public class CustomLayoutManager extends RecyclerView.LayoutManager {
    private float amplifier;
    private float frequency = 1.0f;
    private float phase = 0.0f;
    private float density;
    private int height = 0;
    private int width = 0;

    private Context mContext;

    private int baseHeight;
    private float cx;
    private int mHeight;

    private int mVerticalOffset = 0;

    private int itemCountOnScreen = 10;//在一屏中显示item的数量

    private int left;
    private int childHeight;
    private int childWeight;

    private Path path = new Path();
    private PathMeasure pathMeasure = new PathMeasure();
    private Canvas canvas = new Canvas();
    private Paint paint = new Paint();

    private Paint mLinePaint;
    public CustomLayoutManager(Context mContext) {
        this.mContext = mContext;
        init();
    }

    private void init() {
        this.density = DisplayUtils.getScreenDensity(mContext);
        amplifier = 120.0f * this.density;

        height = DensityUtils.getScreenH(mContext);
        width = DensityUtils.getScreenW(mContext);
        cx = (float) (width / 2);
        mHeight = height / itemCountOnScreen;
        baseHeight = 0 + (mHeight / 2);

        this.mLinePaint = new Paint();
        this.mLinePaint.setStyle(Paint.Style.STROKE);
        this.mLinePaint.setColor(Color.parseColor("#D5AE4F"));
        this.mLinePaint.setStrokeWidth(4.0f * this.density);
        this.mLinePaint.setPathEffect(new DashPathEffect(new float[]{17.0f * this.density, 10.0f * this.density}, 1.0f));
        this.mLinePaint.setAntiAlias(true);


    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        super.onLayoutChildren(recycler, state);
        if(getItemCount() == 0 && state.isPreLayout()){
            return;
        }
        detachAndScrapAttachedViews(recycler); // 分离所有的itemView

        for (int i = 0; i < getItemCount(); i++) {
            View scrap = recycler.getViewForPosition(i); // 根据position获取一个碎片view，可以从回收的view中获取，也可能新构造一个
            measureChildWithMargins(scrap, 0, 0);  // 计算此碎片view包含边距的尺寸
            addView(scrap);
            childHeight = getDecoratedMeasuredHeight(scrap);
            childWeight = getDecoratedMeasuredWidth(scrap);
            left = Math.round((amplifier * ((float) Math.sin(((double) (((phase * 2.0f) * 3.1415927f) / 360.0f)) + (((6.283185307179586d * ((double) frequency)) * ((double) baseHeight)) / ((double) this.height))))) + cx);
            layoutDecorated(scrap,left - (childWeight / 2), baseHeight - (childHeight / 2), (left - (childWeight / 2)) + childWeight, (baseHeight - (childHeight / 2)) + childHeight);
            baseHeight += mHeight;
        }
    }

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public boolean canScrollVertically() {
        return true;
    }

    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        int travel = dy;
        if(mVerticalOffset + dy < 0){
            travel = -mVerticalOffset;
        }else if(mVerticalOffset + dy > baseHeight - getVerticalSpace()){
            travel = baseHeight - getVerticalSpace() - mVerticalOffset;
        }
        mVerticalOffset += travel;
        offsetChildrenVertical(-travel);

        return travel;
    }



    private int getVerticalSpace() {
        // 计算RecyclerView的可用高度，除去上下Padding值
        return getHeight() - getPaddingBottom() - getPaddingTop();
    }

    private int getHorizontalSpace() {
        return getWidth() - getPaddingLeft() - getPaddingRight();
    }

}
